<!DOCTYPE html>
<html lang="en">
<head>
  <title>Vert.x Common SQL interface - Vert.x</title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <meta content="Vert.x is a tool-kit for building reactive applications on the JVM." name="description">
  <link href="http://vertx.io/stylesheets/docs.css" media="screen" rel="stylesheet">
  <link href="http://vertx.io/stylesheets/font-awesome.min.css" media="screen" rel="stylesheet">
  <link href="http://vertx.io/javascripts/styles/rainbow.min.css" media="screen" rel="stylesheet">
  <!-- IE 6-8 support of HTML 5 elements -->
  <!--[if lt IE 9]>
  <script src="http://static.jboss.org/theme/js/libs/html5/pre3.6/html5.min.js"></script>
  <![endif]-->

  <link rel="apple-touch-icon" sizes="57x57" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-57x57.png">
  <link rel="apple-touch-icon" sizes="60x60" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-60x60.png">
  <link rel="apple-touch-icon" sizes="72x72" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-72x72.png">
  <link rel="apple-touch-icon" sizes="76x76" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-76x76.png">
  <link rel="apple-touch-icon" sizes="114x114" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-114x114.png">
  <link rel="apple-touch-icon" sizes="120x120" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-120x120.png">
  <link rel="apple-touch-icon" sizes="144x144" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-144x144.png">
  <link rel="apple-touch-icon" sizes="152x152" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-152x152.png">
  <link rel="apple-touch-icon" sizes="180x180" href="http://vertx.io/assets/favicons/vertx-favicon-7/apple-touch-icon-180x180.png">
  <link rel="icon" type="image/png" href="http://vertx.io/assets/favicons/vertx-favicon-7/favicon-32x32.png" sizes="32x32">
  <link rel="icon" type="image/png" href="http://vertx.io/assets/favicons/vertx-favicon-7/android-chrome-192x192.png" sizes="192x192">
  <link rel="icon" type="image/png" href="http://vertx.io/assets/favicons/vertx-favicon-7/favicon-96x96.png" sizes="96x96">
  <link rel="icon" type="image/png" href="http://vertx.io/assets/favicons/vertx-favicon-7/favicon-16x16.png" sizes="16x16">
  <link rel="manifest" href="http://vertx.io/assets/favicons/vertx-favicon-7/manifest.json">
  <link rel="mask-icon" href="http://vertx.io/assets/favicons/vertx-favicon-7/safari-pinned-tab.svg" color="#5bbad5">
  <meta name="msapplication-TileColor" content="#7d3194">
  <meta name="msapplication-TileImage" content="http://vertx.io/assets/favicons/vertx-favicon-7/mstile-144x144.png">
  <meta name="theme-color" content="#ffffff">

  <link href="http://fonts.googleapis.com/css?family=Ubuntu:400,500,700,400italic" rel="stylesheet" type="text/css">
  <link rel="alternate" type="application/rss+xml" title="RSS"
     href="http://vertx.io/feed.xml">
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-30144458-1', 'auto');
    ga('create', 'UA-71153120-1', 'auto', 'tracker');
    ga('send', 'pageview');
    ga('tracker.send', 'pageview');
  </script>
</head>
<body>

<a href="http://www.reactivemanifesto.org/" id="reactive-manifesto-banner">
  <img style="border: 0; position: fixed; right: 0; top:0; z-index: 9000"
    src="http://d379ifj7s9wntv.cloudfront.net/reactivemanifesto/images/ribbons/we-are-reactive-black-right.png">
</a>

<a id="skippy" class="sr-only sr-only-focusable" href="#content"><div class="container"><span class="skiplink-text">Skip to main content</span></div></a>

<header class="navbar navbar-default navbar-static-top" id="top" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#vertx-navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="http://vertx.io/" class="navbar-brand"><img alt="Brand" src="http://vertx.io/assets/logo-sm.png"></a>
    </div>
    <nav class="collapse navbar-collapse" id="vertx-navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="http://vertx.io/download/">Download</a></li>
        <li><a href="http://vertx.io/docs/">Documentation</a></li>
        <li><a href="https://github.com/vert-x3/wiki/wiki">Wiki</a></li>
        <li><a href="http://vertx.io/community/">Community</a></li>
        <li><a href="http://vertx.io/materials/">Materials</a></li>
        <li><a href="http://vertx.io/blog/">Blog</a></li>        
      </ul>
    </nav>
  </div>
</header>



  <div class="page-header" id="content">
    <div class="container">
      <div class="row">
        <div class="col-sm-12">
          <h1>Vert.x Common SQL interface</h1>
          
        </div>
      </div>
    </div>
  </div>




<div id="content">
  <div class="container docs-content">
    <div class="row">
      <div class="col-sm-12 col-md-push-9 col-md-3 hidden-xs hidden-sm">
        <div id="sidebar" data-spy="affix">
          <ul class="sectlevel1">
<li><a href="#_vert_x_common_sql_interface">Vert.x Common SQL interface</a></li>
<li><a href="#_the_sql_connection">The SQL Connection</a>
<ul class="sectlevel2">
<li><a href="#_auto_commit">Auto-commit</a></li>
<li><a href="#_executing_queries">Executing queries</a></li>
<li><a href="#_prepared_statement_queries">Prepared statement queries</a></li>
<li><a href="#_executing_insert_update_or_delete">Executing INSERT, UPDATE or DELETE</a></li>
<li><a href="#_prepared_statement_updates">Prepared statement updates</a></li>
<li><a href="#_callable_statements">Callable statements</a></li>
<li><a href="#_batch_operations">Batch operations</a></li>
<li><a href="#_executing_other_operations">Executing other operations</a></li>
<li><a href="#_using_transactions">Using transactions</a></li>
<li><a href="#_closing_connections">Closing connections</a></li>
</ul>
</li>
</ul>
        </div>
      </div>
      <div class="col-sm-12 col-md-pull-3 col-md-9">
        <div class="toc hidden-md hidden-lg">
          <h2>Table of Contents</h2>
          <ul class="sectlevel1">
<li><a href="#_vert_x_common_sql_interface">Vert.x Common SQL interface</a></li>
<li><a href="#_the_sql_connection">The SQL Connection</a>
<ul class="sectlevel2">
<li><a href="#_auto_commit">Auto-commit</a></li>
<li><a href="#_executing_queries">Executing queries</a></li>
<li><a href="#_prepared_statement_queries">Prepared statement queries</a></li>
<li><a href="#_executing_insert_update_or_delete">Executing INSERT, UPDATE or DELETE</a></li>
<li><a href="#_prepared_statement_updates">Prepared statement updates</a></li>
<li><a href="#_callable_statements">Callable statements</a></li>
<li><a href="#_batch_operations">Batch operations</a></li>
<li><a href="#_executing_other_operations">Executing other operations</a></li>
<li><a href="#_using_transactions">Using transactions</a></li>
<li><a href="#_closing_connections">Closing connections</a></li>
</ul>
</li>
</ul>
        </div>
        <div class="sect1">
<h2 id="_vert_x_common_sql_interface">Vert.x Common SQL interface</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The common SQL interface is used to interact with Vert.x SQL services.</p>
</div>
<div class="paragraph">
<p>You obtain a connection to the database via the service interface for the specific SQL service that
you are using (e.g. JDBC/MySQL/PostgreSQL).</p>
</div>
<div class="paragraph">
<p>To use this project, add the following dependency to the <em>dependencies</em> section of your build descriptor:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Maven (in your <code>pom.xml</code>):</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-xml" data-lang="xml">&lt;dependency&gt;
  &lt;groupId&gt;io.vertx&lt;/groupId&gt;
  &lt;artifactId&gt;vertx-sql-common&lt;/artifactId&gt;
  &lt;version&gt;3.3.3&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Gradle (in your <code>build.gradle</code> file):</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-groovy" data-lang="groovy">compile 'io.vertx:vertx-sql-common:3.3.3'</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_the_sql_connection">The SQL Connection</h2>
<div class="sectionbody">
<div class="paragraph">
<p>A connection to the database is represented by <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html">SQLConnection</a></code>.</p>
</div>
<div class="sect2">
<h3 id="_auto_commit">Auto-commit</h3>
<div class="paragraph">
<p>When you obtain a connection auto commit is set to <code>true</code>. This means that each operation you perform will effectively
execute in its own transaction.</p>
</div>
<div class="paragraph">
<p>If you wish to perform multiple operations in a single transaction you should set auto commit to false with
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#setAutoCommit(boolean,%20io.vertx.core.Handler)">setAutoCommit</a></code>.</p>
</div>
<div class="paragraph">
<p>When the operation is complete, the handler will be called:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">connection.setAutoCommit(false, { res -&gt;
  if (res.succeeded()) {
    // OK!
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_executing_queries">Executing queries</h3>
<div class="paragraph">
<p>To execute a query use <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#query(java.lang.String,%20io.vertx.core.Handler)">query</a></code></p>
</div>
<div class="paragraph">
<p>The query string is raw SQL that is passed through without changes to the actual database.</p>
</div>
<div class="paragraph">
<p>The handler will be called with the results, represented by <code><a href="../dataobjects.html#ResultSet">ResultSet</a></code> when the query has
been run.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">connection.query("SELECT ID, FNAME, LNAME, SHOE_SIZE from PEOPLE", { res -&gt;
  if (res.succeeded()) {
    // Get the result set
    def resultSet = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code><a href="../dataobjects.html#ResultSet">ResultSet</a></code> instance represents the results of a query.</p>
</div>
<div class="paragraph">
<p>The list of column names are available with <code><a href="../dataobjects.html#ResultSet#getColumnNames()">getColumnNames</a></code>, and the actual results
available with <code><a href="../dataobjects.html#ResultSet#getResults()">getResults</a></code></p>
</div>
<div class="paragraph">
<p>The results are a list of <code>JsonArray</code> instances, one for each row of the results.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">def columnNames = resultSet.columnNames

def results = resultSet.results

results.each { row -&gt;

  def id = row[0]
  def fName = row[1]
  def lName = row[2]
  def shoeSize = row[3]

}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can also retrieve the rows as a list of Json object instances with <code><a href="../dataobjects.html#ResultSet#getRows()">getRows</a></code> -
this can give you a somewhat simpler API to work with, but please be aware that SQL results can contain duplicate
column names - if that&#8217;s the case you should use <code><a href="../dataobjects.html#ResultSet#getResults()">getResults</a></code> instead.</p>
</div>
<div class="paragraph">
<p>Here&#8217;s an example of iterating through the results as Json object instances:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">def rows = resultSet.rows

rows.each { row -&gt;

  def id = row.ID
  def fName = row.FNAME
  def lName = row.LNAME
  def shoeSize = row.SHOE_SIZE

}</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_prepared_statement_queries">Prepared statement queries</h3>
<div class="paragraph">
<p>To execute a prepared statement query you can use
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#queryWithParams(java.lang.String,%20io.vertx.core.json.JsonArray,%20io.vertx.core.Handler)">queryWithParams</a></code>.</p>
</div>
<div class="paragraph">
<p>This takes the query, containing the parameter place holders, and a <code>JsonArray</code> or parameter
values.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">def query = "SELECT ID, FNAME, LNAME, SHOE_SIZE from PEOPLE WHERE LNAME=? AND SHOE_SIZE &gt; ?"
def params = [
  "Fox",
  9
]

connection.queryWithParams(query, params, { res -&gt;

  if (res.succeeded()) {
    // Get the result set
    def resultSet = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_executing_insert_update_or_delete">Executing INSERT, UPDATE or DELETE</h3>
<div class="paragraph">
<p>To execute an operation which updates the database use <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#update(java.lang.String,%20io.vertx.core.Handler)">update</a></code>.</p>
</div>
<div class="paragraph">
<p>The update string is raw SQL that is passed through without changes to the actual database.</p>
</div>
<div class="paragraph">
<p>The handler will be called with the results, represented by <code><a href="../dataobjects.html#UpdateResult">UpdateResult</a></code> when the update has
been run.</p>
</div>
<div class="paragraph">
<p>The update result holds the number of rows updated with <code><a href="../dataobjects.html#UpdateResult#getUpdated()">getUpdated</a></code>, and
if the update generated keys, they are available with <code><a href="../dataobjects.html#UpdateResult#getKeys()">getKeys</a></code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">connection.update("INSERT INTO PEOPLE VALUES (null, 'john', 'smith', 9)", { res -&gt;
  if (res.succeeded()) {

    def result = res.result()
    println("Updated no. of rows: ${result.updated}")
    println("Generated keys: ${result.keys}")

  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_prepared_statement_updates">Prepared statement updates</h3>
<div class="paragraph">
<p>To execute a prepared statement update you can use
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#updateWithParams(java.lang.String,%20io.vertx.core.json.JsonArray,%20io.vertx.core.Handler)">updateWithParams</a></code>.</p>
</div>
<div class="paragraph">
<p>This takes the update, containing the parameter place holders, and a <code>JsonArray</code> or parameter
values.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">def update = "UPDATE PEOPLE SET SHOE_SIZE = 10 WHERE LNAME=?"
def params = [
  "Fox"
]

connection.updateWithParams(update, params, { res -&gt;

  if (res.succeeded()) {

    def updateResult = res.result()

    println("No. of rows updated: ${updateResult.updated}")

  } else {

    // Failed!

  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_callable_statements">Callable statements</h3>
<div class="paragraph">
<p>To execute a callable statement (either SQL functions or SQL procedures) you can use
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#callWithParams(java.lang.String,%20io.vertx.core.json.JsonArray,%20io.vertx.core.json.JsonArray,%20io.vertx.core.Handler)">callWithParams</a></code>.</p>
</div>
<div class="paragraph">
<p>This takes the callable statement using the standard JDBC format <code>{ call func_proc_name() }</code>, optionally including
parameter place holders e.g.: <code>{ call func_proc_name(?, ?) }</code>, a <code>JsonArray</code> containing the
parameter values and finally a <code>JsonArray</code> containing the
output types e.g.: <code>[null, 'VARCHAR']</code>.</p>
</div>
<div class="paragraph">
<p>Note that the index of the output type is as important as the params array. If the return value is the second
argument then the output array must contain a null value as the first element.</p>
</div>
<div class="paragraph">
<p>A SQL function returns some output using the <code>return</code> keyword, and in this case one can call it like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Assume that there is a SQL function like this:
//
// create function one_hour_ago() returns timestamp
//    return now() - 1 hour;

// note that you do not need to declare the output for functions
def func = "{ call one_hour_ago() }"

connection.call(func, { res -&gt;

  if (res.succeeded()) {
    def result = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
<div class="paragraph">
<p>When working with Procedures you and still return values from your procedures via its arguments, in the case you do
not return anything the usage is as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Assume that there is a SQL procedure like this:
//
// create procedure new_customer(firstname varchar(50), lastname varchar(50))
//   modifies sql data
//   insert into customers values (default, firstname, lastname, current_timestamp);

def func = "{ call new_customer(?, ?) }"

connection.callWithParams(func, [
  "John",
  "Doe"
], null, { res -&gt;

  if (res.succeeded()) {
    // Success!
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
<div class="paragraph">
<p>However you can also return values like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Assume that there is a SQL procedure like this:
//
// create procedure customer_lastname(IN firstname varchar(50), OUT lastname varchar(50))
//   modifies sql data
//   select lastname into lastname from customers where firstname = firstname;

def func = "{ call customer_lastname(?, ?) }"

connection.callWithParams(func, [
  "John"
], [
  null,
  "VARCHAR"
], { res -&gt;

  if (res.succeeded()) {
    def result = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that the index of the arguments matches the index of the <code>?</code> and that the output parameters expect to be a
String describing the type you want to receive.</p>
</div>
<div class="paragraph">
<p>To avoid ambiguation the implementations are expected to follow the following rules:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>When a place holder in the <code>IN</code> array is <code>NOT NULL</code> it will be taken</p>
</li>
<li>
<p>When the <code>IN</code> value is NULL a check is performed on the OUT</p>
</li>
<li>
<p>When the <code>OUT</code> value is not null it will be registered as a output parameter</p>
</li>
<li>
<p>When the <code>OUT</code> is also null it is expected that the IN value is the <code>NULL</code> value.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The registered <code>OUT</code> parameters will be available as an array in the result set under the output property.</p>
</div>
</div>
<div class="sect2">
<h3 id="_batch_operations">Batch operations</h3>
<div class="paragraph">
<p>The SQL common interface also defines how to execute batch operations. There are 3 types of batch operations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Batched statements <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#batch(java.util.List,%20io.vertx.core.Handler)">batch</a></code></p>
</li>
<li>
<p>Batched prepared statements <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#batchWithParams(java.lang.String,%20java.util.List,%20io.vertx.core.Handler)">batchWithParams</a></code></p>
</li>
<li>
<p>Batched callable statements <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#batchCallableWithParams(java.lang.String,%20java.util.List,%20java.util.List,%20io.vertx.core.Handler)">batchCallableWithParams</a></code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A batches statement will exeucte a list of sql statements as for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Batch values
def batch = []
batch.add("INSERT INTO emp (NAME) VALUES ('JOE')")
batch.add("INSERT INTO emp (NAME) VALUES ('JANE')")

connection.batch(batch, { res -&gt;
  if (res.succeeded()) {
    def result = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
<div class="paragraph">
<p>While a prepared or callable statement batch will reuse the sql statement and take an list of arguments as for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Batch values
def batch = []
batch.add([
  "joe"
])
batch.add([
  "jane"
])

connection.batchWithParams("INSERT INTO emp (name) VALUES (?)", batch, { res -&gt;
  if (res.succeeded()) {
    def result = res.result()
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_executing_other_operations">Executing other operations</h3>
<div class="paragraph">
<p>To execute any other database operation, e.g. a <code>CREATE TABLE</code> you can use
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#execute(java.lang.String,%20io.vertx.core.Handler)">execute</a></code>.</p>
</div>
<div class="paragraph">
<p>The string is passed through without changes to the actual database. The handler is called when the operation
is complete</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">def sql = "CREATE TABLE PEOPLE (ID int generated by default as identity (start with 1 increment by 1) not null,FNAME varchar(255), LNAME varchar(255), SHOE_SIZE int);"

connection.execute(sql, { execute -&gt;
  if (execute.succeeded()) {
    println("Table created !")
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_using_transactions">Using transactions</h3>
<div class="paragraph">
<p>To use transactions first set auto-commit to false with <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#setAutoCommit(boolean,%20io.vertx.core.Handler)">setAutoCommit</a></code>.</p>
</div>
<div class="paragraph">
<p>You then do your transactional operations and when you want to commit or rollback use
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#commit(io.vertx.core.Handler)">commit</a></code> or
<code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#rollback(io.vertx.core.Handler)">rollback</a></code>.</p>
</div>
<div class="paragraph">
<p>Once the commit/rollback is complete the handler will be called and the next transaction will be automatically started.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Do stuff with connection - updates etc

// Now commit

connection.commit({ res -&gt;
  if (res.succeeded()) {
    // Committed OK!
  } else {
    // Failed!
  }
})</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_closing_connections">Closing connections</h3>
<div class="paragraph">
<p>When you&#8217;ve done with the connection you should return it to the pool with <code><a href="../../groovydoc/io/vertx/groovy/ext/sql/SQLConnection.html#close(io.vertx.core.Handler)">close</a></code>.</p>
</div>
</div>
</div>
</div>

        

        
          <div id="footer">
            <div id="footer-text">
              
                Last updated 2016-09-12 08:38:04 CEST
              
              
            </div>
          </div>
        
      </div>
    </div>
  </div>
</div>

<footer>
  <div class="container">
    <div class="row">
      <div class="col-xs-6 col-sm-3 col-md-3 col-lg-2">
        <h2>Vert.x</h2>
        <ul class="list-unstyled">
          <li><a href="http://vertx.io/">Home</a></li>
          <li><a href="http://vertx.io/download/">Download</a></li>
          <li><a href="http://vertx.io/docs/">Documentation</a></li>
          <li><a href="https://github.com/vert-x3/wiki/wiki">Wiki</a></li>
          <li><a href="http://vertx.io/blog/">Blog</a></li>
          <li><a href="http://vertx.io/vertx2/" class="vertx-2-link">Vert.x 2</a></li>
        </ul>
      </div>
      <div class="col-xs-6 col-sm-3 col-md-3 col-lg-2">
        <h2>Community</h2>
        <ul class="list-unstyled">
          <li><a href="http://vertx.io/community/">Help &amp; Contributors</a></li>
          <li><a href="http://vertx.io/materials/">Learning materials</a></li>
          <li><a href="https://groups.google.com/forum/?fromgroups#!forum/vertx">User Group</a></li>
          <li><a href="https://groups.google.com/forum/?fromgroups#!forum/vertx-dev">Developer Group</a></li>
        </ul>
      </div>

      <div class="col-xs-12 col-sm-6 col-lg-offset-2 col-md-6 copyright">
        <p>Vert.x is open source and dual licensed under the <a href="https://www.eclipse.org/org/documents/epl-v10.php">Eclipse Public License 1.0</a> and <a href="https://www.apache.org/licenses/LICENSE-2.0.html">Apache License 2.0</a>.</p>
        <p>This website is licensed under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">CC BY-SA 3.0 License</a>.<br>
        Design by <a href="http://www.michel-kraemer.com">Michel Kr&auml;mer</a>. <a href="http://www.entypo.com">Entypo pictograms</a> by Daniel Bruce.</p>
        <div class="row">
          <div class="col-xs-12 col-lg-offset-1 col-md-5">
            <a href="http://eclipse.org">
            <img class="logo eclipse-logo" src="http://vertx.io/assets/eclipse_logo_grey_small.png" width="204" height="48">
            </a>
          </div>
          <div class="col-xs-12 col-md-offset-2 col-lg-offset-0 col-md-5">
            <a href="http://cloudbees.com">
            <img class="logo cloudbees-logo" src="http://vertx.io/assets/Button-Built-on-CB-1-grey.png" width="180" height="48">
           </a>
          </div>
          <div class="col-xs-12 col-md-offset-2 col-lg-offset-1 col-md-5 jprofiler">
            <a href="http://www.ej-technologies.com/products/jprofiler/overview.html"
            style="text-decoration:none">
            <img class="logo jprofiler-logo" src="http://vertx.io/assets/jprofiler-logo.png" width="48" height="48"><span class="jprofiler-logo">&nbsp; JPROFILER</span>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</footer>

<script src="http://static.jboss.org/theme/js/libs/jquery/jquery-1.9.1.min.js"></script>
<script src="http://vertx.io/javascripts/bootstrap.min.js"></script>
<script src="http://vertx.io/javascripts/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



<script src="http://vertx.io/javascripts/sidebar.js"></script>


</body>
</html>
